package com.chinamobile.maps.action;

import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.PrintWriter;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.poifs.filesystem.POIFSFileSystem;

import com.chinamobile.maps.dao.IChannelnetworksDAO;
import com.chinamobile.maps.dao.IGridDAO;
import com.chinamobile.maps.dao.IRegionsDAO;
import com.chinamobile.maps.orm.Channelnetworks;
import com.chinamobile.maps.orm.Regions;
import com.chinamobile.maps.util.JsonUtil;
import com.chinamobile.maps.util.LoggerUtil;
import com.chinamobile.maps.util.Struts2Util;
import com.opensymphony.xwork2.ActionSupport;

/**
 * @ClassName: ChannelnetworksAction
 * @Description: TODO
 * @author: frankwong
 * @date: 2012-10-7 下午4:34:20
 * 
 */
public class ChannelnetworksAction extends ActionSupport {

	/**
	 * 
	 */
	private static final long serialVersionUID = -7912489361882419189L;

	public IChannelnetworksDAO channelnetworksDAO = null;
	public IGridDAO gridDao = null;
	public IRegionsDAO regionDao = null;
	/**
	 * @return the channelnetworksDAO
	 */
	public IChannelnetworksDAO getChannelnetworksDAO() {
		return channelnetworksDAO;
	}

	/**
	 * @param channelnetworksDAO the channelnetworksDAO to set
	 */
	public void setChannelnetworksDAO(IChannelnetworksDAO channelnetworksDAO) {
		this.channelnetworksDAO = channelnetworksDAO;
	}

	public static HashMap<String, String> networkTypesMap;

	static {
		networkTypesMap = new HashMap<String, String>();
		networkTypesMap.put("1", "移动主厅");
		networkTypesMap.put("2", "移动合作厅");
		networkTypesMap.put("3", "联通营业厅");
		networkTypesMap.put("4", "电信营业厅");
		networkTypesMap.put("5", "特约代理店");
		networkTypesMap.put("6", "授权销售点");
		networkTypesMap.put("7", "村级便民点");
		networkTypesMap.put("8", "共享渠道");
	}

	public void getRegionTreeCheckItems() {
		HttpServletResponse response = Struts2Util.getResponse();
		response.setContentType("text/json;charset=utf-8");
		PrintWriter out = null;
		try {
			out = (PrintWriter) response.getWriter();
			HttpServletRequest request = Struts2Util.getRequest();
			String data = String.valueOf(request.getParameter("data"));
			String[] strArray = data.split(":");
			String regionName = strArray[0];
			String networkTypesStr = strArray[1];
			String[] networkTypesArray = networkTypesStr.split(",");
            StringBuilder sb = new StringBuilder("from Channelnetworks as c where");
            if(regionName.equals("全南京")){
            	sb.append(" c.regionName is not null");
            }else{
            	sb.append(" c.regionName = '"+regionName+"'");
            }
            sb.append(" and ( ");
            int count = networkTypesArray.length;
            for(int i = 0;i<count;i++){
            	if(i < count -1){
            		sb.append(" c.networkType = '"+networkTypesMap.get(networkTypesArray[i])+"' or ");
            	}else{
            		sb.append(" c.networkType = '"+networkTypesMap.get(networkTypesArray[i])+"'");
            	}
            }
            sb.append(" )");
			List<Channelnetworks> channelnetworks = this.channelnetworksDAO
					.getObjects(sb.toString());
			JSONArray jsonArrayResult = JSONArray.fromObject(channelnetworks);
			out.print(jsonArrayResult.toString());
			out.flush();
			out.close();
		} catch (Exception e) {
			out.print("{\"result\":false}");
			out.flush();
			out.close();
			e.printStackTrace();
		}
	}
	public void getMicroRegionTreeCheckItems() {
		HttpServletResponse response = Struts2Util.getResponse();
		response.setContentType("text/json;charset=utf-8");
		PrintWriter out = null;
		try {
			out = (PrintWriter) response.getWriter();
			HttpServletRequest request = Struts2Util.getRequest();
			String data = String.valueOf(request.getParameter("data"));
			String[] strArray = data.split(":");
			String regionName = strArray[0];
			String networkTypesStr = strArray[1];
			String[] networkTypesArray = networkTypesStr.split(",");
            StringBuilder sb = new StringBuilder("from Channelnetworks as c where");
            if(regionName.equals("全南京")){
            	int count = networkTypesArray.length;
                for(int i = 0;i<count;i++){
                	if(networkTypesArray[i]!=null && !networkTypesArray[i].equals("")){
                		String[] vailStr = networkTypesArray[i].split("Z");
                    	if(i < count -1){
                    		sb.append("( c.regionName = '"+vailStr[0]+"' and c.networkType = '"+vailStr[1]+"') or ");
                    	}else{
                    		sb.append("( c.regionName = '"+vailStr[0]+"' and c.networkType = '"+vailStr[1]+"')");
                    	}
                	}
                }
            }else{
            	int count = networkTypesArray.length;
                for(int i = 0;i<count;i++){
                	if(networkTypesArray[i] != null && !networkTypesArray[i].equals("")){
                		String[] vailStr = networkTypesArray[i].split("Z");
                    	if(i < count -1){
                    		sb.append("( c.microRegion = '"+vailStr[0]+"' and c.networkType = '"+vailStr[1]+"') or ");
                    	}else{
                    		sb.append("( c.microRegion = '"+vailStr[0]+"' and c.networkType = '"+vailStr[1]+"')");
                    	}
                	}
                }
            }
			List<Channelnetworks> channelnetworks = this.channelnetworksDAO
					.getObjects(sb.toString());
			JSONArray jsonArrayResult = JSONArray.fromObject(channelnetworks);
			out.print(jsonArrayResult.toString());
			out.flush();
			out.close();
		} catch (Exception e) {
			out.print("{\"result\":false}");
			out.flush();
			out.close();
			e.printStackTrace();
		}
	}
	public void getChannelsList(){
		HttpServletResponse response = Struts2Util.getResponse();
		response.setContentType("text/json;charset=utf-8");
		PrintWriter out = null;
		try {
			out = (PrintWriter) response.getWriter();
			HttpServletRequest request = Struts2Util.getRequest();
			int pageNumber = Integer.parseInt((request.getParameter("pageNumber")));
			int pageSize = Integer.parseInt((request.getParameter("pageSize")));
			String keyword = String.valueOf(request.getParameter("keyword"));
			List<Channelnetworks> channelnetworksList ;
			if(keyword == null || keyword.equals("")){
				channelnetworksList = this.channelnetworksDAO.getChannelnetworks();
			}else{
				channelnetworksList = this.channelnetworksDAO.findByKeyword(keyword);
			}
			int count;
			if(channelnetworksList == null){
				out.print("[{}]");
				out.flush();
				out.close();
				return;
			}else{
				count = channelnetworksList.size();
				List<Channelnetworks> channelnetworksResult = new ArrayList<Channelnetworks>();
				for (int i = pageSize * (pageNumber - 1); i < count
						&& i < pageSize * pageNumber; i++) {
					channelnetworksResult.add(channelnetworksList.get(i));
				}
				Map<String, Object> jsonMap = new HashMap<String, Object>();
				jsonMap.put("total", count);
				jsonMap.put("rows", channelnetworksResult);
				JSONObject jsonObject = JSONObject.fromObject(jsonMap,JsonUtil.jsonConfig);
				out.print(jsonObject.toString());
				out.flush();
				out.close();
			}
		} catch (Exception e) {
			out.print("{\"result\":false}");
			out.flush();
			out.close();
			e.printStackTrace();
		}
	}
	
	public void addChannel(){
		HttpServletResponse response = Struts2Util.getResponse();
		response.setContentType("text/html;charset=utf-8");
		PrintWriter out = null;
		try {
			out = (PrintWriter) response.getWriter();
			HttpServletRequest request = Struts2Util.getRequest();
			Channelnetworks channelnetworks = new Channelnetworks();
			String pic_Url = String.valueOf(request.getParameter("pic_Url"));
			String id  = String.valueOf(request.getParameter("id"));
			String sn = String.valueOf(request.getParameter("sn"));
			String regionName = String.valueOf(request.getParameter("regionName"));
			String regionProperty = String.valueOf(request.getParameter("regionProperty"));
			String channelCode = String.valueOf(request.getParameter("channelCode"));
			String maketingUnit = String.valueOf(request.getParameter("maketingUnit"));
			String microRegion = String.valueOf(request.getParameter("microRegion"));
			String channelType= String.valueOf(request.getParameter("channelType"));
			String networkType= String.valueOf(request.getParameter("networkType"));
			String name= String.valueOf(request.getParameter("name"));
			String outsourcing= String.valueOf(request.getParameter("outsourcing"));
			String address= String.valueOf(request.getParameter("address"));
			String manager= String.valueOf(request.getParameter("manager"));
			String phone= String.valueOf(request.getParameter("phone"));
			String longitudeStr = request.getParameter("longitude");
			String latitudeStr = request.getParameter("latitude");
			if(longitudeStr == null || longitudeStr.equals("")){
				longitudeStr = "0.0";
			}
			if(latitudeStr == null || latitudeStr.equals("")){
				latitudeStr = "0.0";
			}	
			BigDecimal longitude;
			BigDecimal latitude ;
			try{
				 longitude = BigDecimal.valueOf(Double.parseDouble(longitudeStr));
			}catch(Exception e){
				longitude = BigDecimal.valueOf(0.0);
			}
			try{
				latitude = BigDecimal.valueOf(Double.parseDouble(latitudeStr));
			}catch(Exception e){
				latitude = BigDecimal.valueOf(0.0);
			}
			String specialty= String.valueOf(request.getParameter("specialty"));
			String regionEnvironment= String.valueOf(request.getParameter("regionEnvironment"));
			String area= String.valueOf(request.getParameter("area"));
			if(id == null || id.equals("null")){
				channelnetworks.setPic_Url(pic_Url);
				channelnetworks.setSn(sn);
				channelnetworks.setRegionName(regionName);
				channelnetworks.setRegionProperty(regionProperty);
				channelnetworks.setChannelCode(channelCode);
				channelnetworks.setMaketingUnit(maketingUnit);
				channelnetworks.setMicroRegion(microRegion);
				channelnetworks.setChannelType(channelType);
				channelnetworks.setNetworkType(networkType);
				channelnetworks.setName(name);
				channelnetworks.setOutsourcing(outsourcing);
				channelnetworks.setAddress(address);
				channelnetworks.setManager(manager);
				channelnetworks.setPhone(phone);
				channelnetworks.setLongitude(longitude);
				channelnetworks.setLatitude(latitude);
				channelnetworks.setSpecialty(specialty);
				channelnetworks.setRegionEnvironment(regionEnvironment);
				channelnetworks.setArea(area);
				this.channelnetworksDAO.addChannelnetworks(channelnetworks);
				operationByChannel(1,networkType,regionName,microRegion);
				LoggerUtil.LogInfo("增加渠道网点:"+channelnetworks.getName());				
			}else{
				channelnetworks = this.channelnetworksDAO.getChannelnetworksById(Integer.parseInt(id));
				channelnetworks.setPic_Url(pic_Url);
				channelnetworks.setSn(sn);
				channelnetworks.setRegionName(regionName);
				channelnetworks.setRegionProperty(regionProperty);
				channelnetworks.setChannelCode(channelCode);
				channelnetworks.setMaketingUnit(maketingUnit);
				channelnetworks.setMicroRegion(microRegion);
				channelnetworks.setChannelType(channelType);
				channelnetworks.setNetworkType(networkType);
				channelnetworks.setName(name);
				channelnetworks.setOutsourcing(outsourcing);
				channelnetworks.setAddress(address);
				channelnetworks.setManager(manager);
				channelnetworks.setPhone(phone);
				channelnetworks.setLongitude(longitude);
				channelnetworks.setLatitude(latitude);
				channelnetworks.setSpecialty(specialty);
				channelnetworks.setRegionEnvironment(regionEnvironment);
				channelnetworks.setArea(area);
				this.channelnetworksDAO.updateChannelnetworks(channelnetworks);
				LoggerUtil.LogInfo("更新渠道网点:"+channelnetworks.getName());
			}
			out.print("{\"result\":true}");
			out.flush();
			out.close();
		} catch (Exception e) {
			out.print("{\"result\":false}");
			out.flush();
			out.close();
			e.printStackTrace();
		}
	}
	
	public void deleteChannel(){
		HttpServletResponse response = Struts2Util.getResponse();
		response.setContentType("text/json;charset=utf-8");
		PrintWriter out = null;
		try {
			out = (PrintWriter) response.getWriter();
			HttpServletRequest request = Struts2Util.getRequest();
			int id = Integer.parseInt((request.getParameter("id")));
			Channelnetworks c = this.channelnetworksDAO.getChannelnetworksById(id);
			operationByChannel(-1,c.getNetworkType(),c.getRegionName(),c.getMicroRegion());
			this.channelnetworksDAO.delChannelnetworks(id);
			LoggerUtil.LogWarn("删除渠道网点:"+c.getName());
			out.print("{\"result\":true}");
			out.flush();
			out.close();

		} catch (Exception e) {
			out.print("{\"result\":false}");
			out.flush();
			out.close();
			e.printStackTrace();
		}
	}
	
	public void editChannel(){
		HttpServletResponse response = Struts2Util.getResponse();
		response.setContentType("text/json;charset=utf-8");
		PrintWriter out = null;
		try {
			out = (PrintWriter) response.getWriter();
			HttpServletRequest request = Struts2Util.getRequest();
			int id = Integer.parseInt((request.getParameter("id")));
			Channelnetworks channelnetworks = this.channelnetworksDAO.getChannelnetworksById(id);
			String sn = String.valueOf(request.getParameter("sn"));
			String regionName = String.valueOf(request.getParameter("regionName"));
			String regionProperty = String.valueOf(request.getParameter("regionProperty"));
			String channelCode = String.valueOf(request.getParameter("channelCode"));
			String maketingUnit = String.valueOf(request.getParameter("maketingUnit"));
			String microRegion = String.valueOf(request.getParameter("microRegion"));
			String channelType= String.valueOf(request.getParameter("channelType"));
			String networkType= String.valueOf(request.getParameter("networkType"));
			String name= String.valueOf(request.getParameter("name"));
			String outsourcing= String.valueOf(request.getParameter("outsourcing"));
			String address= String.valueOf(request.getParameter("address"));
			String manager= String.valueOf(request.getParameter("manager"));
			String phone= String.valueOf(request.getParameter("phone"));
			BigDecimal longitude = BigDecimal.valueOf(Double.parseDouble(request.getParameter("longitude")));
			BigDecimal latitude = BigDecimal.valueOf(Double.parseDouble(request.getParameter("latitude")));
			String specialty= String.valueOf(request.getParameter("loginName"));
			String regionEnvironment= String.valueOf(request.getParameter("loginName"));
			String area= String.valueOf(request.getParameter("loginName"));
			
			channelnetworks.setSn(sn);
			channelnetworks.setRegionName(regionName);
			channelnetworks.setRegionProperty(regionProperty);
			channelnetworks.setChannelCode(channelCode);
			channelnetworks.setMaketingUnit(maketingUnit);
			channelnetworks.setMicroRegion(microRegion);
			channelnetworks.setChannelType(channelType);
			channelnetworks.setNetworkType(networkType);
			channelnetworks.setName(name);
			channelnetworks.setOutsourcing(outsourcing);
			channelnetworks.setAddress(address);
			channelnetworks.setManager(manager);
			channelnetworks.setPhone(phone);
			channelnetworks.setLongitude(longitude);
			channelnetworks.setLatitude(latitude);
			channelnetworks.setSpecialty(specialty);
			channelnetworks.setRegionEnvironment(regionEnvironment);
			channelnetworks.setArea(area);
			
			this.channelnetworksDAO.updateChannelnetworks(channelnetworks);
			LoggerUtil.LogInfo("编辑渠道网点:"+channelnetworks.getName());
			out.print("{\"result\":1}");
			out.flush();
			out.close();
		} catch (Exception e) {
			out.print("{\"result\":0}");
			out.flush();
			out.close();
			e.printStackTrace();
		}
	}
	
	public void deleteAllChannels(){
		HttpServletResponse response = Struts2Util.getResponse();
		response.setContentType("text/json;charset=utf-8");
		PrintWriter out = null;
		try {
			out = (PrintWriter) response.getWriter();
			HttpServletRequest request = Struts2Util.getRequest();
			if(this.channelnetworksDAO.deleteAll()){
				LoggerUtil.LogWarn("删除所有渠道网点");
				out.print("{\"result\":true}");
				out.flush();
				out.close();
			}else{
				out.print("{\"result\":false}");
				out.flush();
				out.close();
			}
		} catch (Exception e) {
			out.print("{\"result\":false}");
			out.flush();
			out.close();
			e.printStackTrace();
		}
	}
	
	public void exportExcel() throws IOException {
		HttpServletResponse response = Struts2Util.getResponse();
		HttpServletRequest request = Struts2Util.getRequest();
		HSSFWorkbook wb = null;
		POIFSFileSystem fs = null;
		String fileName = request.getSession().getServletContext()
				.getRealPath("/")
				+ "resources/templates/template.xls";
		fileName = fileName.replace("\\", "/");
		fs = new POIFSFileSystem(new FileInputStream(fileName));
		wb = new HSSFWorkbook(fs);
		
		HSSFSheet sheet = wb.getSheetAt(0);
		List<Channelnetworks> list = this.channelnetworksDAO.getChannelnetworks();
		int count = list.size();
		int j =0;
        for(int i=1;i<count;i++){
        	HSSFRow row = sheet.createRow(i);
        	j=0;
        	row.createCell(j++).setCellValue(list.get(i-1).getSn());
        	row.createCell(j++).setCellValue(list.get(i-1).getRegionName());
        	row.createCell(j++).setCellValue(list.get(i-1).getRegionProperty());
        	row.createCell(j++).setCellValue(list.get(i-1).getChannelCode());
        	row.createCell(j++).setCellValue(list.get(i-1).getMaketingUnit());
        	row.createCell(j++).setCellValue(list.get(i-1).getMicroRegion());
        	row.createCell(j++).setCellValue(list.get(i-1).getChannelType());
        	row.createCell(j++).setCellValue(list.get(i-1).getNetworkType());
        	row.createCell(j++).setCellValue(list.get(i-1).getName());
        	row.createCell(j++).setCellValue(list.get(i-1).getOutsourcing());
        	row.createCell(j++).setCellValue(list.get(i-1).getAddress());
        	row.createCell(j++).setCellValue(list.get(i-1).getManager());
        	row.createCell(j++).setCellValue(list.get(i-1).getPhone());
        	if(list.get(i-1).getLongitude() == null){
        		row.createCell(j++,HSSFCell.CELL_TYPE_NUMERIC);
        	}else{
        		row.createCell(j++,HSSFCell.CELL_TYPE_NUMERIC).setCellValue(list.get(i-1).getLongitude().doubleValue());
        	}
        	if(list.get(i-1).getLatitude() == null){
        		row.createCell(j++,HSSFCell.CELL_TYPE_NUMERIC);
        	}else{
        		row.createCell(j++,HSSFCell.CELL_TYPE_NUMERIC).setCellValue(list.get(i-1).getLatitude().doubleValue());
        	}
        	row.createCell(j++).setCellValue(list.get(i-1).getSpecialty());
        	row.createCell(j++).setCellValue(list.get(i-1).getRegionEnvironment());
        	row.createCell(j++).setCellValue(list.get(i-1).getArea());
        }
		/* 如果文件存在 */
		response.reset();
		response.setContentType("application/ms-excel");
		response.setCharacterEncoding("UTF-8");
		response.addHeader("Content-Disposition",
				"attachment; filename=export.xls");
		ByteArrayOutputStream baos = new ByteArrayOutputStream();
		wb.write(baos); 
        baos.flush(); 
        int fileLength = baos.size();
		response.setContentLength(baos.size());
		/* 如果文件长度大于0 */
		if (fileLength != 0) {
			/* 创建输入流 */
			ServletOutputStream servletOS = response.getOutputStream();
			servletOS.write(baos.toByteArray());
			servletOS.flush();
			servletOS.close();
		}
	}
	
	public void searchByKeywordForPage(){
		HttpServletResponse response = Struts2Util.getResponse();
		response.setContentType("text/json;charset=utf-8");
		PrintWriter out = null;
		try {
			out = (PrintWriter) response.getWriter();
			HttpServletRequest request = Struts2Util.getRequest();
			int pageNumber = Integer.parseInt((request.getParameter("pageNumber")));
			int pageSize = Integer.parseInt((request.getParameter("pageSize")));
			String keyWord = String.valueOf((request.getParameter("keyWord")));
			List<Channelnetworks> channelnetworksList = this.channelnetworksDAO.findByKeyword(keyWord);
			int count;
			if(channelnetworksList == null){
				out.print("{\"result\":false}");
				return;
			}else{
				count = channelnetworksList.size();
				List<Channelnetworks> channelnetworksResult = new ArrayList<Channelnetworks>();
				for (int i = pageSize * (pageNumber - 1); i < count
						&& i < pageSize * pageNumber; i++) {
					channelnetworksResult.add(channelnetworksList.get(i));
				}
				Map<String, Object> jsonMap = new HashMap<String, Object>();
				jsonMap.put("total", count);
				jsonMap.put("rows", channelnetworksResult);
				JSONObject jsonObject = JSONObject.fromObject(jsonMap,JsonUtil.jsonConfig);
				out.print(jsonObject.toString());
				out.flush();
				out.close();
			}
		} catch (Exception e) {
			out.print("{\"result\":false}");
			out.flush();
			out.close();
			e.printStackTrace();
		}
	}
	
	public void searchByKeyword(){
		HttpServletResponse response = Struts2Util.getResponse();
		response.setContentType("text/json;charset=utf-8");
		PrintWriter out = null;
		try {
			out = (PrintWriter) response.getWriter();
			HttpServletRequest request = Struts2Util.getRequest();
			String keyWord = String.valueOf((request.getParameter("keyWord")));
			List<Channelnetworks> channelnetworksList = this.channelnetworksDAO.findByKeyword(keyWord);
			if(channelnetworksList == null){
				out.print("{\"result\":false}");
				return;
			}else{
				JSONArray jsonArrayResult = JSONArray.fromObject(channelnetworksList);
				out.print(jsonArrayResult.toString());
				out.flush();
				out.close();
			}
		} catch (Exception e) {
			out.print("{\"result\":false}");
			out.flush();
			out.close();
			e.printStackTrace();
		}
	}
	
	public void uploadXls(){
		HttpServletResponse response = Struts2Util.getResponse();
		response.setContentType("text/json;charset=utf-8");
		PrintWriter out = null;
		try {
			out = (PrintWriter) response.getWriter();
			HttpServletRequest request = Struts2Util.getRequest();
			String file = String.valueOf((request.getParameter("file")));
		} catch (Exception e) {
			out.print("{\"result\":false}");
			out.flush();
			out.close();
			e.printStackTrace();
		}
	}

	private boolean isFileUploaded(File file) {
		return true;
	}
	
	public void getGridTreeCheckItems(){
		HttpServletResponse response = Struts2Util.getResponse();
		response.setContentType("text/json;charset=utf-8");
		PrintWriter out = null;
		try {
			out = (PrintWriter) response.getWriter();
			HttpServletRequest request = Struts2Util.getRequest();
			String data = String.valueOf(request.getParameter("data"));
			String[] strArray = data.split(":");
			String regionName = strArray[0];
			String networkTypesStr = strArray[1];
			String[] networkTypesArray = networkTypesStr.split(",");
            StringBuilder sb = new StringBuilder("from Channelnetworks as c where");
            if(regionName.equals("全南京")){
            	sb.append("[{}]");
            }else{
            	int count = networkTypesArray.length;
                for(int i = 0;i<count;i++){
                	if(networkTypesArray[i]!=null && !networkTypesArray[i].equals("")){
                		String[] vailStr = networkTypesArray[i].split("Z");
                		int gridId = this.gridDao.getObjects("from Grid as g where g.name='"+vailStr[0]+"'").get(0).getId();
                		List<Regions> result = this.regionDao.getObjects("from Regions as r where r.gridId="+gridId);
                    	if(i < count -1){
                    		for(int j=0;j<result.size();j++){
                    			sb.append("( c.microRegion = '"+result.get(j).getName()+"' and c.networkType = '"+vailStr[1]+"') or ");
                    		}
                    	}else{
                    		int j=0;
                    		for(;j<result.size()-1;j++){
                    			sb.append("( c.microRegion = '"+result.get(j).getName()+"' and c.networkType = '"+vailStr[1]+"') or ");
                    		}
                    		sb.append("( c.microRegion = '"+result.get(j).getName()+"' and c.networkType = '"+vailStr[1]+"')");
                    	}
                	}
                }
            }
			List<Channelnetworks> channelnetworks = this.channelnetworksDAO
					.getObjects(sb.toString());
			JSONArray jsonArrayResult = JSONArray.fromObject(channelnetworks);
			out.print(jsonArrayResult.toString());
			out.flush();
			out.close();
		} catch (Exception e) {
			out.print("{\"result\":false}");
			out.flush();
			out.close();
			e.printStackTrace();
		}
	}
//	    return (file.get().equals("application/vnd.ms-excel")) || 
//	      (file.getContentType().equals("application/ms-excel")) || 
//	      (file.getContentType().equals("application/msexcel")) || 
//	      (file.getContentType().equals("application/octetstream"));

	/**
	 * @Title:       operationByChannel
	 * @Description: TODO
	 * @param   type 1:add -1:remove
	 * @return:      Regions   
	 * @throws
	 */
	public void operationByChannel(int type,String networkType,String regionName,String microRegionName){
		try{
			Regions mainRegion = this.regionDao.getRegionsById(1);
			Regions region = this.regionDao.getRegionsByName(regionName);
			Regions microRegion = this.regionDao.getRegionsByName(microRegionName);
			
			if(networkType.equals("移动主厅")){
				region.setMainHallCount(region.getMainHallCount()+type);
				mainRegion.setMainHallCount(mainRegion.getMainHallCount()+type);
				microRegion.setMainHallCount(microRegion.getMainHallCount()+type);
			}else if(networkType.equals("移动合作厅")){
				region.setCooperativeHallCount(region.getCooperativeHallCount()+type);
				mainRegion.setCooperativeHallCount(mainRegion.getCooperativeHallCount()+type);
				microRegion.setCooperativeHallCount(microRegion.getCooperativeHallCount()+type);
			}else if(networkType.equals("特约代理店")){
				region.setSpecialAgentCount(region.getSpecialAgentCount()+type);
				mainRegion.setSpecialAgentCount(mainRegion.getSpecialAgentCount()+type);
				microRegion.setSpecialAgentCount(microRegion.getSpecialAgentCount()+type);
			}else if(networkType.equals("授权销售点")){
				region.setAuthorizedPointCount(region.getAuthorizedPointCount()+type);
				mainRegion.setAuthorizedPointCount(mainRegion.getAuthorizedPointCount()+type);
				microRegion.setAuthorizedPointCount(microRegion.getAuthorizedPointCount()+type);
			}else if(networkType.equals("村级便民点")){
				region.setVillagePointCount(region.getVillagePointCount()+type);
				mainRegion.setVillagePointCount(mainRegion.getVillagePointCount()+type);
				microRegion.setVillagePointCount(microRegion.getVillagePointCount()+type);
			}else if(networkType.equals("联通营业厅")){
				region.setUnicomHallCount(region.getUnicomHallCount()+type);
				mainRegion.setUnicomHallCount(mainRegion.getUnicomHallCount()+type);
				microRegion.setUnicomHallCount(microRegion.getUnicomHallCount()+type);
			}else if(networkType.equals("电信营业厅")){
				region.setTelecomHallCount(region.getTelecomHallCount()+type);
				mainRegion.setTelecomHallCount(mainRegion.getTelecomHallCount()+type);
				microRegion.setTelecomHallCount(microRegion.getTelecomHallCount()+type);
			}else if(networkType.equals("共享渠道")){
				region.setShareChannelCount(region.getShareChannelCount()+type);
				mainRegion.setShareChannelCount(mainRegion.getShareChannelCount()+type);
				microRegion.setShareChannelCount(microRegion.getShareChannelCount()+type);
			}else{
			}
			this.regionDao.updateRegions(mainRegion);
			this.regionDao.updateRegions(region);
			this.regionDao.updateRegions(microRegion);
		}catch(Exception e){
			System.out.println("error");
		}
		
	}
	/**
	 * @return the gridDao
	 */
	public IGridDAO getGridDao() {
		return gridDao;
	}

	/**
	 * @param gridDao the gridDao to set
	 */
	public void setGridDao(IGridDAO gridDao) {
		this.gridDao = gridDao;
	}

	/**
	 * @return the regionDao
	 */
	public IRegionsDAO getRegionDao() {
		return regionDao;
	}

	/**
	 * @param regionDao the regionDao to set
	 */
	public void setRegionDao(IRegionsDAO regionDao) {
		this.regionDao = regionDao;
	}

	
}
